<template>
	<view class="mine-page">
		<view class="user-header" :style="{ backgroundImage: `url(/static/header-bg.png)` }">
			<image class="avatar" src="/static/avatar.png" mode="cover"></image>
			<view class="user-info">
				<text class="greeting" v-if="isLogin">{{ `HI~${userInfo.username}` }}</text>
				<text class="greeting" v-else>HI~欢迎回来</text>
				<button class="login-btn" v-if="!isLogin" @click="goLogin">登录</button>
				<view class="user-tag" v-if="isLogin">普通用户</view>
			</view>
			<view class="stats">
				<view class="stat-item">关注3</view>
				<view class="stat-item">话题0</view>
			</view>
		</view>
		<view class="func-area">
			<view class="func-item" @click="handleCollect">
				<image src="/static/collect.png" mode="cover"></image>
				<text>收藏</text>
			</view>
			<view class="func-item" @click="handleHistory">
				<image src="/static/history.png" mode="cover"></image>
				<text>历史</text>
			</view>
			<view class="func-item" @click="handleSetting">
				<image src="/static/settings.png" mode="cover"></image>
				<text>设置</text>
			</view>
		</view>
		<view class="ad-banner">
			<image src="/static/ad.jpg" mode="widthFix"></image>
		</view>
		<view class="publish-area">
			<view class="publish-desc">
				<text>已有385,893人</text>
				<text>在这里发布身边的新鲜事</text>
			</view>
			<button class="publish-btn" @click="handlePublish">我也要发布</button>
			<view class="go-browse" @click="goBrowse">先去逛逛</view>
		</view>
	</view>
</template>

<script>
	import {
		isLogin,
		getUserInfo,
		logout
	} from '../../utils/auth';

	export default {
		data() {
			return {
				userInfo: getUserInfo(),
				isLogin: isLogin()
			};
		},
		onShow() {
			this.userInfo = getUserInfo();
			this.isLogin = isLogin();
			uni.setTabBarItem({
				index: 3,
				text: this.isLogin ? '我的' : '未登录'
			});
		},
		methods: {
			goLogin() {
				uni.navigateTo({
					url: "/pages/Login/Login"
				});
			},
			handleCollect() {
				if (!this.isLogin) {
					uni.showToast({
						title: '请先登录',
						icon: 'none'
					});
					return;
				}
				uni.showToast({
					title: '收藏页待实现',
					icon: 'none'
				});
			},
			handleHistory() {
				if (!this.isLogin) {
					uni.showToast({
						title: '请先登录',
						icon: 'none'
					});
					return;
				}
				uni.showToast({
					title: '历史页待实现',
					icon: 'none'
				});
			},
			handleSetting() {
				if (!this.isLogin) {
					uni.showToast({
						title: '请先登录',
						icon: 'none'
					});
					return;
				}
				uni.showModal({
					title: '设置',
					content: '确定要退出登录吗？',
					success: (res) => {
						if (res.confirm) {
							logout();
						}
					}
				});
			},
			handlePublish() {
				if (!this.isLogin) {
					uni.showToast({
						title: '请先登录',
						icon: 'none'
					});
					return;
				}
				uni.showToast({
					title: '发布功能待实现',
					icon: 'none'
				});
			},
			goBrowse() {
				uni.showToast({
					title: '首页待实现',
					icon: 'none'
				});
			}
		}
	};
</script>

<style scoped>
	.mine-page {
		display: flex;
		flex-direction: column;
		min-height: 100vh;
		background-color: #f5f5f5;
	}

	/* 顶部用户信息区 */
	.user-header {
		width: 100%;
		height: 300rpx;
		background-size: cover;
		background-position: center;
		position: relative;
		padding: 30rpx;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		gap: 20rpx;
	}

	.avatar {
		width: 120rpx;
		height: 120rpx;
		border-radius: 50%;
	}

	.user-info {
		display: flex;
		flex-direction: column;
		gap: 10rpx;
	}

	.greeting {
		font-size: 32rpx;
		color: #fff;
	}

	.login-btn {
		background-color: #ff5722;
		color: #fff;
		font-size: 24rpx;
		padding: 8rpx 20rpx;
		border-radius: 20rpx;
		width: 120rpx;
	}

	.user-tag {
		font-size: 24rpx;
		color: #fff;
		opacity: 0.8;
	}

	.stats {
		display: flex;
		justify-content: space-around;
		position: absolute;
		bottom: 20rpx;
		left: 0;
		width: 100%;
		color: #fff;
		font-size: 28rpx;
	}

	.stat-item {
		text-align: center;
	}

	/* 功能按钮区（强制横向排列） */
	.func-area {
		display: flex;
		flex-direction: row;
		/* 显式指定横向布局 */
		justify-content: space-around;
		align-items: center;
		width: 100%;
		padding: 30rpx 0;
		background-color: #fff;
		margin-top: 20rpx;
		box-sizing: border-box;
	}

	.func-item {
		display: flex;
		flex-direction: column;
		align-items: center;
		gap: 10rpx;
	}

	.func-item image {
		width: 60rpx;
		height: 60rpx;
	}

	.func-item text {
		font-size: 24rpx;
		color: #333;
	}

	/* 广告横幅 */
	.ad-banner {
		padding: 0 20rpx;
		margin-bottom: 20rpx;
	}

	.ad-banner image {
		width: 100%;
		border-radius: 8rpx;
	}

	/* 发布入口 */
	.publish-area {
		flex: 1;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 40rpx 0;
	}

	.publish-desc {
		display: flex;
		flex-direction: column;
		align-items: center;
		color: #999;
		font-size: 24rpx;
		margin-bottom: 30rpx;
	}

	.publish-btn {
		background-color: #007AFF;
		color: #fff;
		border-radius: 30rpx;
		padding: 15rpx 40rpx;
		margin-bottom: 20rpx;
	}

	.go-browse {
		font-size: 24rpx;
		color: #999;
	}
</style>